<?php
include("/var/www/html/new/d/common/header.html");
include("/var/www/html/new/d/common/left.html");
include("/var/www/html/new/d/common/page.php");
include("/var/www/html/new/d/class/abc/PFTCoreAPI.class.php");

if(isset($_SESSION['alliance'])) exit('联盟禁止配置分销商价格');

$ac=$GLOBALS['_ac'];
$pw=$GLOBALS['_pw'];
$s = new SoapClient(null,array("location" => "http://localhost/open/openService/pft_insideMX.php","uri" => "www.16u.com?ac_16u=$ac|pw_16u=$pw|auth_16u=true"));

$sid = $_SESSION['sid'];// 会员ID
$did = $_REQUEST["did"];// 分销商ID
$prodname = $_REQUEST['prodname'];




// 分销商名称
$sel="select dname,account from pft_member where id=$did limit 1";
$GLOBALS['le']->query($sel);
$GLOBALS['le']->fetch_assoc();
$dname = $GLOBALS['le']->f("dname");
$daccount = $GLOBALS['le']->f("account");// 分销商账号

$pageSize = $_REQUEST['pageSize'] ? $_REQUEST["pageSize"]:30;
$currentPage = $_REQUEST["currentPage"] ? $_REQUEST["currentPage"]:1;
$nowPage =  ($currentPage-1)*$pageSize;




$date = date('Y-m-d');

// 获取会员分销的产品
$pids = $prod = array();
$sql  = "select e.pid,p.p_name,e.sid,e.lvl,m.dname,e.aids from `pft_p_apply_evolute` e left join uu_products p on e.pid=p.id left join pft_member m on e.sid=m.id left join uu_land l on p.contact_id=l.id where e.fid=$sid and e.status=0 and e.active=1 and sourceid<>$did and lvl<3 and !find_in_set($did,e.aids) and l.status=1 and p.apply_limit=1 and p.p_status in (0,1) and p_name like '%$prodname%' order by p_name desc limit $nowPage,$pageSize";
$GLOBALS['le']->query($sql);
while($row=$GLOBALS['le']->fetch_assoc()){

	$pStorage = PFTCoreAPI::pStorage($s, $_SESSION['saccount'], $row['pid'], $row['sid'], $date);
    if($pStorage['js']['p']==-1) continue;// 获取不到价格则无法销售
	$pk = $row['pid'].'_'.$row['sid'];
    $pids[$pk] = $row['pid'];
    $prod[$pk] = array(
        'id'  => $row['pid'],
		'fp'  => $pStorage['js']['p'],
		'ls'  => $pStorage['ls']['p'],
		'aid' => $row['sid'],
		'aids'=> $row['aids'],
		'dname'    => $row['dname'],
        'p_name'   => $row['p_name'],
        'disabled' => 'disabled="true"',
    );
	
}
$total = 0;
if(count($prod)){// 总记录
	$sql="select count(e.pid) as total from `pft_p_apply_evolute` e left join uu_products p on e.pid=p.id left join uu_land l on p.contact_id=l.id where e.fid=$sid and e.status=0 and e.active=1 and e.sourceid<>$did and lvl<3 and !find_in_set($did,e.aids) and l.status=1 and p.p_status in (0,1)";
	echo $sql;
	$GLOBALS['le']->query($sql);
	$GLOBALS['le']->fetch_assoc();
	$total = $GLOBALS['le']->f('total');
}

if(count($pids)){// 分销商分销了会员的分销产品
    $pids = implode(',', $pids);
    $sql="select pid,aids from `pft_p_apply_evolute` where sid=$sid and fid=$did and status=0 and pid in ($pids)";
    $GLOBALS['le']->query($sql);
    while($row=$GLOBALS['le']->fetch_assoc()){
		$aids  = explode(',', $row['aids']);
		$count = count($aids);
		if(isset($aids[$count-2])){
			$aid = $aids[$count-2]; 
			$pk  = $row['pid'].'_'.$aid;
			if(!isset($prod[$pk])) continue;
			$prod[$pk]['checked'] = 'checked="checked"';
			$prod[$pk]['disabled'] = '';
			$pStorage = PFTCoreAPI::pStorage($s, $daccount, $row['pid'], $sid, $date, 0);
			$prod[$pk]['pri'] = ($pStorage['js']['p']==-1) ? '':$pStorage['js']['p'];
		}
		
    }
}
include("/var/www/html/new/d/tpl/priceset_sale_set_t.html");
include("/var/www/html/new/d/common/footer.html");